package com.adjy.pathbuilders.screens;

import java.util.ArrayList;
import java.util.List;

import android.view.MotionEvent;

import com.adjy.pathbuilders.R;
import com.adjy.pathbuilders.database.GameDatabase;
import com.adjy.pathbuilders.objects.button.Button;
import com.adjy.pathbuilders.objects.button.ImageButton;
import com.adjy.pathbuilders.objects.button.TextImageButton;
import com.adjy.pathbuilders.objects.button.listeners.BackToMainMenuButtonListener;
import com.adjy.pathbuilders.objects.button.listeners.MutableButtonListener;
import com.adjy.pathbuilders.rendering.DisplayConverter;
import com.adjy.pathbuilders.rendering.GameCanvas;
import com.adjy.pathbuilders.rendering.loading.AnimationFactory;
import com.adjy.pathbuilders.rendering.loading.WeakReferenceBitmap;
import com.adjy.pathbuilders.sounds.SoundManager;
import com.adjy.pathbuilders.state.Game;

public class OptionMenu implements GameScreen {
	
	private final static float VOLUME_MAX = 100;
	
	//serao deletadas futuramente as variaveis abaixo
	private AnimationFactory factory;
	private Game game;
	
	private final float BAR_Y = 160 * DisplayConverter.getScreenRatioHeight();
	private final float BAR_HEIGHT = 83 * DisplayConverter.getScreenRatioHeight();
	private final float BAR_X = 300 * DisplayConverter.getScreenRatioWidth();
	private final float BAR_WIDTH = 300 * DisplayConverter.getScreenRatioWidth();
		
	private final float SOUND_Y = 160;
	private final float SOUND_X = 300 - 100;
	
	private static float volumePercentage; // FIXME: This should be gotten from DB

	private List<Button> buttons;

	private static boolean mute;
	
	public OptionMenu(AnimationFactory factory, Game game) {
		this.factory = factory;
		this.game = game;
		this.buttons = new ArrayList<Button>();
		mute = false;
		
		volumePercentage = GameDatabase.getInstance().getVolume();
		mute = GameDatabase.getInstance().getMuted();
		
	}
	
	private void createButtons() {
		WeakReferenceBitmap s1 = new WeakReferenceBitmap(factory, "with_sound_selected");
		WeakReferenceBitmap us1 = new WeakReferenceBitmap(factory, "with_sound_unselected");
		WeakReferenceBitmap s2 = new WeakReferenceBitmap(factory, "without_sound_selected");
		WeakReferenceBitmap us2 = new WeakReferenceBitmap(factory, "without_sound_unselected");
		MutableButtonListener muteButtonListener = new MutableButtonListener(s1, us1, s2, us2) {
			
			@Override
			public void onButtonPressed2() {
				OptionMenu.mute = false;
				SoundManager.getInstance().setVolume(OptionMenu.volumePercentage);
				setState(1);
			}
			
			@Override
			public void onButtonPressed1() {
				OptionMenu.mute = true;
				SoundManager.getInstance().setVolume(0);
				setState(2);
			}
		};
		ImageButton muteButton = new ImageButton(SOUND_X, SOUND_Y, muteButtonListener, s1, us1);
		this.buttons.add(muteButton);
		muteButtonListener.setButton(muteButton);
		muteButtonListener.setState(mute ? 2 : 1);
		
		this.buttons.add(new TextImageButton(200, 390, new BackToMainMenuButtonListener(game),
				new WeakReferenceBitmap(factory, "menu_button_selected"),
				new WeakReferenceBitmap(factory, "menu_button_unselected"),
				game.getContext().getString(R.string.back)));
	}
	
	public void render(GameCanvas mcanvas, float timeElapsed) {		
		mcanvas.drawBackground(factory.getImage("main_menu_bg"));
        mcanvas.drawBitmapWithConversion(factory.getImage("bar_volume"), 300, 150);        
        mcanvas.drawBitmapWithConversion(factory.getImage("bar_set_volume"),
        		300 + 300 * (VOLUME_MAX - volumePercentage) / VOLUME_MAX,
        		150);
		
		//mcanvas.drawText("Volume" , 150, 100, msons);
		for(Button b : this.buttons)
			b.render(mcanvas, timeElapsed);
	}
	
	public void input(MotionEvent ev){
        setBar(ev.getX(), ev.getY());
        for(Button b : this.buttons)
			b.input(ev);
	}
	
	private void setBar(float x, float y) {
		
		if(x >= BAR_X && x <= BAR_X + BAR_WIDTH
				&& y >= BAR_Y && y <= BAR_Y + BAR_HEIGHT){
			setVolumePercentage(VOLUME_MAX - VOLUME_MAX * (x - BAR_X) / BAR_WIDTH);
		}
			
	}
	
	private void setVolumePercentage(float newVolume) {
		volumePercentage = newVolume;
		if(!mute)SoundManager.getInstance().setVolume(volumePercentage);
		else SoundManager.getInstance().setVolume(0);
	}
	
	@Override
	public void load() {
		factory.loadImages("main_menu_bg", "bar_set_volume", "bar_volume",
				"with_sound_selected", "with_sound_unselected", "without_sound_selected", "without_sound_unselected");
		createButtons();
	}

	@Override
	public void release() {
		GameDatabase.getInstance().setVolume((int) volumePercentage, mute);
		
		factory.releaseImages("main_menu_bg", "bar_set_volume", "bar_volume",
				"with_sound_selected", "with_sound_unselected", "without_sound_selected", "without_sound_unselected");
		this.buttons.clear();
	}

	@Override
	public void step(float timePassed) {
		// TODO Auto-generated method stub
		
	}
	
	public float getVolumePercentage() {
		return volumePercentage;
	}
}
